﻿using System.IO;

namespace Vision.VHelper
{
    public static class Logger
    {

        private static readonly object _lock = new object();
        private static string _logFilePath;

        static Logger()
        {
            string projectRootDirectory = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..//.."));

            // 在项目根目录下创建 Log 文件夹
            string logDirectory = Path.Combine(projectRootDirectory, "Log");

            if (!Directory.Exists(logDirectory))
            {
                Directory.CreateDirectory(logDirectory);
            }
            _logFilePath = Path.Combine(logDirectory, $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.txt");
        }

        public static void Log(string message)
        {
            lock (_lock)
            {
                using (StreamWriter writer = new StreamWriter(_logFilePath, true))
                {
                    writer.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} - {message}");
                }
            }
        }

        public static void Log(Exception ex)
        {
            Log($"Exception: {ex.Message}\nStack Trace: {ex.StackTrace}");
        }

    }
}
